package my.edu.model.bills.fund.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import my.edu.model.bills.fund.entity.OtherExpenseOrder;
import my.edu.model.bills.fund.entity.OtherIncomeOrderDetail;
import my.edu.model.bills.fund.entity.dto.ExpenseDetailDTO;
import org.springframework.stereotype.Service;
import my.edu.model.bills.fund.entity.OtherExpenseOrderDetail;
import my.edu.model.bills.fund.mapper.OtherExpenseOrderDetailMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OtherExpenseOrderDetailServiceImpl extends ServiceImpl<OtherExpenseOrderDetailMapper, OtherExpenseOrderDetail> implements IOtherExpenseOrderDetailService{

    @Override
    public void removeByOrderId(List<Long> longs) {
        remove(Wrappers.<OtherExpenseOrderDetail>lambdaQuery().in(OtherExpenseOrderDetail::getOtherExpenseOrderId,longs));
    }

    @Override
    @Transactional
    public void updateByDTO(List<ExpenseDetailDTO> expenseDetailDTOList, OtherExpenseOrder byId) {
        remove(Wrappers.<OtherExpenseOrderDetail>lambdaQuery().eq(OtherExpenseOrderDetail::getOtherExpenseOrderId, byId.getId()));
        saveBatchByDTO(expenseDetailDTOList, byId);
    }

    @Override
    @Transactional
    public void saveBatchByDTO(List<ExpenseDetailDTO> expenseDetailDTOList, OtherExpenseOrder otherExpenseOrder) {
        List<OtherExpenseOrderDetail> list = expenseDetailDTOList.stream().map(i -> {
            OtherExpenseOrderDetail otherExpenseOrderDetail = BeanUtil.copyProperties(i, OtherExpenseOrderDetail.class);
            otherExpenseOrderDetail.setOtherExpenseOrderId(otherExpenseOrder.getId());
            return otherExpenseOrderDetail;
        }).toList();
        saveBatch(list);
    }
}
